package public

import (
	"context"
	"errors"

	"ruikc.com/server/api/internal/db"
	"ruikc.com/server/api/internal/svc"
	"ruikc.com/server/api/internal/types"
	"ruikc.com/server/models"
	"ruikc.com/server/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type InitLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewInitLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InitLogic {
	return &InitLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InitLogic) Init(req *types.LoginUser) (resp string, err error) {
	data := models.User{Id: 1}
	has := db.GetData(&data)
	if has {
		return "", errors.New("管理员账号已经存在")
	}
	data.Username = req.Username
	data.Salt = utils.RandStr(6)
	data.Password = utils.MD5(utils.MD5(req.Password) + data.Salt)
	data.IsAdmin = 10
	data.RoleId = 0
	data.IsUse = 10

	if !db.InsertData(&data) {
		return "", errors.New("管理员账号创建失败")
	}

	return "管理员账号创建成功", nil
}
